Beginners Programming
Demos
This is yet another example of the use of the speech programs kindly devised by Stefan Pendl in previous news letters, known over here in as ‘ Cribbing ‘ other’s ideas ‘!!
If you have an number of programs involving the use of this speech routine, then you will see by running the first program below, you do not need to include all the speech code on your main program. The text$ is passed onto a Speech.tkn and read, then the speech is aborted after a period set by the variable time based on the length of the CommandLine$. An extra 500 milliseconds is added to overcome any loss by the use of INT(etc).
The warnings needed, in case the user does not have the necessary MS Agent files installed are included at the start.
[Editor: Gordon sent me the stm.dll for inclusion in the files section. Since it is already a part of Janet's Blood Presure Simulator, I will point you to that archive for the dll. It can also be downloaded from the Liberty Basic FTP site at: http://babek.info/libertybasicfiles/lbf_menu.html]
Gordon Sweet
‘ **** PROGRAM 1 TO TEST A SPEECH.TKN OF BELOW ****
print:print tab(10);"PLEASE WAIT FOR ALL THE WORDS TO BE SPOKEN"
text$ = " Now is the time for all good men to come to the aid of the party."
run$ = "speech.tkn "+ text$
run run$
cls : print : print tab(20);"OK - ALL DONE"
===========================================================
' PROGRAM 2 Create a SPEECH.TKN of this program
' *** NEEDS stm.dll ***
' Text to Speech using MS Agent
' <http://www.microsoft.com/msagent/downloads/user.asp>
' and
' <http://www.microsoft.com/msagent/downloads/developer.asp>
'
' based on tts_tool.c by Martin Goebel
' <http://www.john.findlay1.btinternet.co.uk/OLE/ole.htm>
'
' using stm.dll and ttw.dll by John White
' <http://www.btinternet.com/~yoingco/yoingco/speech/index.htm>
'
' by Stefan Pendl <megabit@t-online.at>
'
' Revision History
' ================
'
' Version | Date | Name | Reason
' --------|-------------|--------------|-----------------------------------
' 1.0 | 1.Oct.2003 | Stefan Pendl | Initial
nomainwin
'constants:
TEXT.BUFFER.SIZE = 5000
NEW.TTS.ALWAYS = 1
USE.DEFAULT.VOLUME = 0
USE.DEFAULT.PITCH = 0
USE.DEFAULT.SPEED = 0
USE.TEXT = 0
CLEAR.TEXTSPEECH.BUFFER = 3
'variables to preset:
voiceTotal = 0
currentVoice(1) = 0
voiceNumber = 0
volume(1) = 0
pitch(1) = 0
speed(1) = 0
SourceFlag(1) = USE.TEXT
ClearFlag(1) = CLEAR.TEXTSPEECH.BUFFER
TTSFlag(1) = NEW.TTS.ALWAYS
VolumeFlag(1) = USE.DEFAULT.VOLUME
PitchFlag(1) = USE.DEFAULT.PITCH
SpeedFlag(1) = USE.DEFAULT.SPEED
'at the very beginning of your app to check if speech is installed:
open "stm.dll" for dll as #speech
speechOpen = 1
rtn = BeginSpeech(TEXT.BUFFER.SIZE)
if rtn = 1 then
voiceTotal = CountVoices()
if voiceTotal > 0 then
call EnumVoiceData
vdPtr = GetVoiceData(currentVoice(1))
else
notice "Speech API 4 Error";chr$(13);_
"Speech API 4 Runtime and/or Voice Engines NOT Installed SEE ";chr$(13);_
"MS Agent support files are downloadable from [http://www.microsoft.com/msagent/downloads/user.asp]";chr$(13);_
"MS Agent documentation is found at [http://www.microsoft.com/msagent/downloads/developer.asp]"
goto [quit]
end if
else
notice "Speech Library Error";chr$(13);_
"MS Agent Core NOT Installed."
goto [quit]
end if
'You should check the commandline length at the beginning
'and set the buffer size accordingly:
textLength = len(CommandLine$)
if (textLength * 2) > TEXT.BUFFER.SIZE then TEXT.BUFFER.SIZE = (textLength * 2) + 100
rtn = BeginSpeech(TEXT.BUFFER.SIZE)
'to speak the string no temporary file is needed:
Speech$ = CommandLine$ ' Using any text parameter
rtn = SpeakString(Speech$)
UpperLeftX = DisplayWidth-100: UpperLeftY = 10
WindowWidth = 100 : WindowHeight = 60
button #s, "STOP ", [STOP], UL 20, 2
open "Speech" for dialog as #s
#s "trapclose [STOP]"
mainH=hWnd(#s) : gosub [boxhold]
' time based on length of CommandLine$ providing the
' opening SPEED etc constants are not altered.
time = int(len(Speech$)*.07*1000+500)
timer time, [STOP]
wait
[STOP]
timer 0
'before ending your app:
if speechOpen = 1 then
rtn = EndSpeech()
close #speech
end if
close #s : END
[boxhold]
open "user32" for dll as #user ' prevents losing box
toTop=(-1 or 0)
flags=_SWP_NOMOVE or _SWP_NOSIZE
calldll #user,"SetWindowPos",mainH as ushort,toTop as short,_
0 as short,0 as short,0 as short,0 as short,flags as ushort,_
result as void
close #user : return
'functions and subs:
function BeginSpeech(BufferSize)
calldll #speech, "Begin_Speech",_
BufferSize as long,_ ' memory buffer size in bytes; >= 5000
BeginSpeech as long ' 1 ... success; 0 ... error
end function
function StopSpeech()
calldll #speech, "Stop_Speech",_
StopSpeech as long ' 1 ... success; 0 ... error
end function
function GetVoiceData(VoiceIndex)
calldll #speech, "Get_VoiceData",_
VoiceIndex as long,_
GetVoiceData as long
end function
function SpeakString(Text$)
TextBuffer$ = Text$ + chr$(0)
sourceFlag = SourceFlag(1)
clearFlag = ClearFlag(1)
VoiceIndex = currentVoice(1)
ttsFlag = TTSFlag(1)
volumeFlag = VolumeFlag(1)
Volume = volume(1)
pitchFlag = PitchFlag(1)
Pitch = pitch(1)
speedFlag = SpeedFlag(1)
Speed = speed(1)
calldll #speech, "Speak_String",_
sourceFlag as long,_
TextBuffer$ as ptr,_
clearFlag as long,_
VoiceIndex as long,_
ttsFlag as long,_
volumeFlag as long,_
Volume as long,_
pitchFlag as long,_
Pitch as long,_
speedFlag as long,_
Speed as long,_
SpeakString as long
end function
function EndSpeech()
calldll #speech, "End_Speech",_
EndSpeech as long
end function
sub EnumVoiceData
calldll #speech, "Enum_VoiceData",_
result as long
end sub
function CountVoices()
calldll #speech, "Count_Voices",_
CountVoices as long ' 0 if no voices are installed
end function ' else the number of installed voices